<?php
$AppUI->savePlace();

// pull all the key types
$sql = 'SELECT syskey_id,syskey_name FROM syskeys ORDER BY syskey_name';
$keys = arrayMerge( array( 0 => '- Select Type -' ), db_loadHashList( $sql ) );

$sql = 'SELECT * FROM syskeys, sysvals WHERE sysval_key_id = syskey_id ORDER BY sysval_title';
$values = db_loadList( $sql );

$sysval_id = isset( $_GET['sysval_id'] ) ? $_GET['sysval_id'] : 0;

$titleBlock = new CTitleBlock( 'System Lookup Values', 'preferences.png' );
$titleBlock->addButton('System Admin', '?m=system');
$titleBlock->show();

if ($canEdit) {
?>
<script language="javascript">
function delIt(id) {
	if (confirm( 'Are you sure you want to delete this?' )) {
		f = document.sysValFrm;
		f.del.value = 1;
		f.sysval_id.value = id;
		f.submit();
	}
}

function doEditValue(id)
{
	var r = $('idValue_'+id);
	var t = parseInt(r.cells[1].getAttribute('data'), 10);
	var s = $('idSysValType');
	for (i = 0; i < s.options.length; i++)
		if (s.options[i].value == t) {
			s.selectedIndex = i;
			break;
		}
	$('idSysValID').value = id;
	$('idSysValTitle').value = unescape(r.cells[2].getAttribute('data'));
	$('idSysValValue').value = unescape(r.cells[3].getAttribute('data'));
	Dialog('idEditValue').show();
}
</script>

<div id="idEditValue" class="popup" style="visibility: hidden" autohide="click">
<form name="sysValFrm" method="post" action="?m=system&u=syskeys&a=do_sysval_aed">
<input type="hidden" name="del" value="0" />
<input id="idSysValID" type="hidden" name="sysval_id" value="0" />
<table border="0" cellpadding="8" cellspacing="0">
<tr>
	<td bgcolor="#E0E0E0" colspan="2"><b>System value information</b></td>
</tr>
<tr>
	<td>Type</td>
	<td valign="top">
		<?=arraySelect( $keys, 'sysval_key_id', 'id="idSysValType" size="1" class="text" style="width: 300px"', 0)?>
	</td>
</tr>
<tr>
	<td>Title</td>
	<td>
		<input id="idSysValTitle" type="text" name="sysval_title" value="" class="text" style="width: 300px"/>
	</td>
</tr>
<tr>
	<td valign="top">Value</td>
	<td>
		<textarea id="idSysValValue" name="sysval_value" rows="4" style="width: 300px"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
		<input type="submit" value="<?=$AppUI->_('Save')?>" class="button"/>
		<input type="button" value="<?=$AppUI->_('Cancel')?>" class="button" onclick="Dialog('idEditValue').hide()"/>
	</td>
</tr>
</table>
</form>
</div>
<? } ?>

<table border="0" cellpadding="2" cellspacing="0" width="100%" class="list">
<tr>
	<th width="12px">&nbsp;</th>
	<th><?=$AppUI->_('Key Type')?></th>
	<th><?=$AppUI->_('Title')?></th>
	<th colspan="2"><?=$AppUI->_('Values')?></th>
	<th width="12px">&nbsp;</th>
</tr>
<?php

function showRow($id=0, $key=0, $title='', $value='') {
	GLOBAL $canEdit, $sysval_id, $AppUI, $keys;
	$s = '<tr id="idValue_'.$id.'" class="second">';
	if ($id == 0 && $canEdit) {
		$s .= '<td valign="top">'.
			'<a href="javascript:doEditValue(0)"><img src="images/common_add.gif" title="'.$AppUI->_('Add').'"></a></td>'.
			'<td colspan="3" data="0"></td><td data=""></td><td data=""></td>';
	} else {
		$s .= '<td valign="top">';
		if ($canEdit) {
			$s .= '<a href="javascript:doEditValue('.$id.')" title="'.$AppUI->_('Edit').'">'
				. '<img src="images/edit.gif"/>'
				. '</a>';
			$s .= '</td>';
		}
		$s .= '<td valign="top" data="'.$key.'">'.$keys[$key].'</td>';
		$s .= '<td valign="top" data="'.rawurlencode(htmlentities($title)).'">'.dPformSafe($title).'</td>';
		$s .= '<td valign="top" colspan="2" data="'.rawurlencode(htmlentities($value)).'">'.str_replace("\n", '<br/>', $value).'</td>';
		$s .= '<td valign="top">';
		if ($canEdit) {
			$s .= '<a href="javascript:delIt('.$id.')" title="'.$AppUI->_('Delete').'">'
				. '<img src="images/delete.gif"/>'
				. '</a>';
		}
		$s .= '</td>';
	}
	$s .= '</tr>';
	return $s;
}

foreach ($values as $row) {
	echo showRow( $row['sysval_id'], $row['sysval_key_id'], $row['sysval_title'], $row['sysval_value'] );
}
echo showRow();
?>
</table>
